from config.configs import GOAT_ERP_HOST, GOAT_ERP_PORT
from tools.funciton import write_xlsx, skuId_to_size, spuId_to_articleNumber, timestamp_switch_date, read_xlsx_file
import pymongo
import os
from config.configs import GOAT_ERP_HOST, GOAT_ERP_PORT, DATA_PATH,DOWNLOAD_PATH

korea = pymongo.MongoClient(GOAT_ERP_HOST, GOAT_ERP_PORT).goat_warehouse_korea
w2 = pymongo.MongoClient(GOAT_ERP_HOST, GOAT_ERP_PORT).goat_warehouse_w2


def check_wrong_datas(cond, name):
    """核对韩国仓实际入库上架数量和预约数量不一致数据"""
    li = []
    pipeline = [
        {
            '$match':
                cond
        },
        {
            '$project': {
                'merchantName': 1,
                'spus': {
                    '$objectToArray': "$spus"
                },
                'createdAt': 1,
                'expressNo': "$appointNo"
            }
        },
        {
            '$unwind': {
                'path': "$spus"
            }
        },
        {
            '$project': {
                'spuid': "$spus.k",
                'skudata': {
                    '$objectToArray': "$spus.v"
                },
                'merchantName': 1,
                'expressNo': 1,
                'createdAt': 1
            }
        },
        {
            '$unwind': {
                'path': "$skudata"
            }
        },
        {
            '$project': {
                'sku': "$skudata.k",
                'spu': "$spuid",
                'expressNo': 1,
                'merchantName': 1,
                'createdAt': 1,
                'num': "$skudata.v"
            }
        },
        {
            '$project': {
                'merchantName': 1,
                'expressNo': 1,
                'spu': 1,
                'sku': 1,
                'createdAt': 1,
                'num': "$num.appointNum"
            }
        },
        {
            '$lookup': {
                'from': "stockinorders",
                'localField': "expressNo",
                'foreignField': "appointNo",
                'as': "result"
            }
        },
        {
            '$unwind': {
                'path': "$result"
            }
        },
        {
            '$project': {
                'merchantName': 1,
                'expressNo': 1,
                'spu': 1,
                'sku': 1,
                'createdAt': 1,
                'num': 1,
                'receive_spu': {
                    '$objectToArray': "$result.receiptSkuCounts"
                }
            }
        },
        {
            '$unwind': {
                'path': "$receive_spu"
            }
        },
        {
            '$project': {
                'merchantName': 1,
                'expressNo': 1,
                'spu': 1,
                'sku': 1,
                'createdAt': 1,
                'num': 1,
                'r_spu': "$receive_spu.k",
                'r_sku': "$receive_spu.v"
            }
        },
        {
            '$project': {
                'merchantName': 1,
                'expressNo': 1,
                'spu': 1,
                'sku': 1,
                'createdAt': 1,
                'num': 1,
                'r_spu': 1,
                'r_skus': {
                    '$objectToArray': "$r_sku"
                }
            }
        },
        {
            '$unwind': {
                'path': "$r_skus"
            }
        },
        {
            '$project': {
                'merchantName': 1,
                'expressNo': 1,
                'spu': 1,
                'sku': 1,
                'createdAt': 1,
                'num': 1,
                'r_spu': 1,
                'r_sku': "$r_skus.k",
                'r_num': "$r_skus.v.receiptNum",
                'dif': {
                    '$subtract': [
                        "$r_skus.v.receiptNum",
                        "$num"
                    ]
                }
            }
        },
        {
            '$match':
                {
                    'dif': {
                        '$ne': 0
                    },
                    'r_num': {
                        "$ne": None
                    }
                }
        },
        {
            '$project':
                {
                    '_id': 0,
                    'r_spu': 0,
                    'r_sku': 0
                }
        }
    ]
    for i in korea.appointments.aggregate(pipeline=pipeline):
        li.append(list(i.values()))
    for z in li:
        z[1] = timestamp_switch_date(z[1])
        z[4] = spuId_to_articleNumber('korea', int(z[4]))
        z[3] = skuId_to_size('korea', int(z[3]))
        z.insert(3, z[4])
        z.pop(5)
        z.append(z[1])
        z.pop(1)
    write_xlsx(['商家名称', '预约单号', '货号', '尺码', '预约数量', '实收数量', '差异', '预约时间'], li, name)


# check_wrong_datas({"merchantCode":"00015"}, '00015预约数量与入库数量不一致')


